const fun={
	//验证手机号
	telfun(tel) {
		let panten=/^1[3|4|5|7|8][0-9]{9}$/;
		if(panten.test(tel)){
			return true;
		}else{
			return false;
		}
	},
	//验证固定电话号码
	checkphone(phone) {
		let paten=/^((d{3,4})|d{3,4}-|s)?d{7,14}$/;
		if(paten.test(phone)){
			return true;
		}else{
			return false;
		}
	},
	//验证身份证
	checkisIDCard(str) {
		let reg=/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
		if(reg.test(str)){
			return true;
		}else{
			return false;
		}
	},
	//验证邮箱
	checkemail(email) {
		let paten=/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
		if(paten.test(email)){
			return true;
		}else{
			return false;
		}
	},
	// js截取中文字符串
	/**
	 * @str 字符串
	 * @len 长度
	 * */
	cutstr(str, len) {
		var str_length = 0;
		var str_len = 0;
		var str_cut = new String();
		str_len = str.length;
		for (var i = 0; i < str_len; i++) {
			var a = str.charAt(i);
			str_length++;
			if (escape(a).length > 4) {
				//中文字符的长度经编码之后大于4  
				str_length++;
			}
			str_cut = str_cut.concat(a);
			if (str_length >= len) {
				str_cut = str_cut.concat("...");
				return str_cut;
			}
		}
		//如果给定字符串小于指定长度，则返回源字符串；  
		if (str_length < len) {
			return str;
		}
	},
	//把时间戳转换成日期
    timestampToTime(timestamp,str) {
		var date = new Date(timestamp );//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var Y = date.getFullYear();
		var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
		var D = date.getDate()<10?'0'+date.getDate():date.getDate();
		var h = date.getHours();
		var m = date.getMinutes();
		var s = date.getSeconds();
		//return Y+M+D+h+m+s;
		return Y+str+M+str+D;
   },
   //把时间戳转换成日期
    timestampToTime2(timestamp,str) {
   		var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
   		var Y = date.getFullYear();
   		var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
   		var D = date.getDate();
   		var h = date.getHours();
   		var m = date.getMinutes();
   		var s = date.getSeconds();
   		//return Y+M+D+h+m+s;
   		return Y+str+M;
   },
	formatDate(date, fmt) {
		if (typeof date == 'string') {
			return date;
		}

		if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";

		if (!date || date == null) {
			date=new Date()
		}else{
			var date = new Date(date);
		}
		var o = {
			'M+': date.getMonth() + 1, // 月份
			'd+': date.getDate(), // 日
			'h+': date.getHours(), // 小时
			'm+': date.getMinutes(), // 分
			's+': date.getSeconds(), // 秒
			'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
			'S': date.getMilliseconds() // 毫秒
		}
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
		for (var k in o) {
			if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
		}
		return fmt;
   },

   //根据经纬度计算两点之间的距离
   jsjul(lat1, lng1, lat2, lng2) {
	   var radLat1 = lat1 * Math.PI / 180.0;
	   var radLat2 = lat2 * Math.PI / 180.0;
	   var a = radLat1 - radLat2;
	   var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
	   var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	   s = s * 6378.137;
	   s = Math.round(s * 10000) / 10000;
	   return s;
   },
   //处理日期格式的 去掉时分秒 
   /**
	* dates = 2020-12-14 10:00:00
	* */
   timeString(dates) {
   	let panten=/\d{4}-\d{1,2}-\d{1,2}/g;
   	let newDate=panten.exec(dates);
   	return newDate;
   },
   //根据生日计算年龄
   /**
	* strBirthday传入格式 2020-04-15
	* */
   calcAge(strBirthday) {
   	var returnAge=0;
   	if(strBirthday){
   		var brr=strBirthday.split(" ");
   		console.log(brr);
   		var strBirthdayArr = brr[0].split("-");
   		var birthYear = strBirthdayArr[0];
   		var birthMonth = strBirthdayArr[1];
   		var birthDay = strBirthdayArr[2];
   		//获取当前日期
   		var d = new Date();
   		var nowYear = d.getFullYear();
   		var nowMonth = d.getMonth() + 1;
   		var nowDay = d.getDate();
   		if (nowYear == birthYear) {
   			returnAge = 0; //同年 则为0岁
   		} else {
   			var ageDiff = nowYear - birthYear; //年之差
   			if (ageDiff > 0) {
   				if (nowMonth == birthMonth) {
   					var dayDiff = nowDay - birthDay; //日之差
   					if (dayDiff < 0) {
   						returnAge = ageDiff - 1;
   					} else {
   						returnAge = ageDiff;
   					}
   				} else {
   					var monthDiff = nowMonth - birthMonth; //月之差
   					if (monthDiff < 0) {
   						returnAge = ageDiff - 1;
   					} else {
   						returnAge = ageDiff;
   					}
   				}
   			} else {
   				returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
   			}
   		}
   	}
   	return returnAge; //返回周岁年龄
   },
   //处理富文本编辑器图片
   srcreplace(a) {
   	var b = /<img [^>]*src=['"]([^'"]+)[^>]*>/g;
   	var s = a.match(b);
   	var arrs=[];
   	for (var i = 0; i < s.length; i++) {
   		var srcImg = s[i].replace(b, '$1');//取src面的内容
   		arrs.push(srcImg);
   	}
   	return arrs;
   },
   //计算时间间隔，也就是时间段。例:03:34:45
   //mss 为时间戳差值，单位为ms
   formatDuring (mss) {
	   var hours = parseInt(mss / (1000 * 60 * 60));
	   var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
	   var seconds = parseInt((mss % (1000 * 60)) / 1000);
	   var str =
		 pad(hours)+
		 ":" +
		 pad(minutes) +
		 ":" +
		 pad(seconds);
	   return str;
	 },
	 //隐藏店铺名称部分文字,至多隐藏四个字,
	 formatShopName(name) {
	 		var str_array = name.split("");
	 		
	 		if(str_array.length < 3){
	 			
	 		}else if(str_array.length == 3){
	 			name = name.replace(str_array[1],"*");
	 		}else if(str_array.length > 3){
	 			var index = 0;
	 			for (var i = 1; i < str_array.length; i+=2) {
	 				if(i!=str_array.length-1 && i!=str_array.length-2 && index<4){
	 					name = name.replace(str_array[i],"*");
	 					index ++;
	 					console.log('name=='+name);
	 				}
	 			}
	 		}
	 		
	 		return name;
	 	},
		//保留两位小数
		  keepTwoDecimal(num) {  
			   var result = parseFloat(num);  
			   if (isNaN(result)) {  
				  //alert('传递参数错误，请检查！');  
				  return false;  
			   }  
			   result = Math.round(num * 100) / 100;  
			 return result;  
		},
		//去除中文空格
	trimqb(sValue) {
		if(sValue!=undefined){
		  var lastValue=sValue.replace(/(^\s*)|(\s*$)/g,"");
		  return lastValue;
		}
	},
	//计算两日期返回的天数
	DateDiff(sDate1, sDate2){ //sDate1和sDate2是2002-12-18格式
		var aDate, oDate1, oDate2, iDays
		aDate = sDate1.split("-")
		oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2002格式
		aDate = sDate2.split("-")
		oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
		iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
		return iDays
	},
	//计算两日期返回的天数
	 getDaysBetween(dateString1,dateString2){
	    var  startDate = Date.parse(dateString1);
	    var  endDate = Date.parse(dateString2);
	    if (startDate>endDate){
	        return 0;
	    }
	    if (startDate==endDate){
	        return 1;
	    }
	    var days=(endDate - startDate)/(1*24*60*60*1000);
	    return  days;
	}
}

export default fun;